const Promise = require('bluebird');

/**
 * 代理结算
 */
exports.handle = function ({body, user_id}, {models: {Users, AgencysSettles}, cache: {settings, dicts}, controller}) {
    let {id, opt} = body;

    return Users.findOne({where: {id: user_id, level: 'Partner'}}).then(function (user) {
        if (!user) {
            return {success: false, message: '没有权限操作'}
        }
        if (opt === 'amount') {
            return AgencysSettles.amountV2(id, user_id);
        } else if (opt === 'payed') {
            return AgencysSettles.payedV2(id, user_id);
        }
        return {success: false, message: '不支持该操作'};
    });
}

/**
 * 批量代理结算
 */
exports.batchHandle = function ({body, user_id}, {models: {Users, AgencysSettles}, cache: {settings, dicts}, controller}) {
    let {opt} = body;

    return Users.findOne({where: {id: user_id, level: 'Partner'}}).then(function (user) {
        if (!user) {
            return {success: false, message: '没有权限操作'}
        }
        if (opt === 'amount') {
            return AgencysSettles.batchAmountV2(user);
        } else if (opt === 'payed') {
            return AgencysSettles.batchPayedV2(user);
        }
        return {success: false, message: '不支持该操作'};
    });
}